package com.lsh.controller;

import com.lsh.entity.Result;
import com.lsh.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {
@Autowired
private MemberService memberService;
    /**
     * 会员数了折线图
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        //先把上一年十二个月添加到数组之中
        List<String> stringList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,-1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            Date time = calendar.getTime();
            String format = simpleDateFormat.format(time);
            stringList.add(format);
        }
        //查询这12个月的会员数量
        List<Integer> count = memberService.findByMonth(stringList);
        //把数据封装成前端所需要的
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("months",stringList);
        resultMap.put("memberCount",count);
        return new Result(true,"会员数量查询成功",resultMap);
    }
}
